package com.xuecheng.content.controller;


import cn.hutool.core.util.RandomUtil;
import com.xuecheng.commons.model.vo.ResponseResult;
import com.xuecheng.content.config.SmsTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;

@Slf4j
@RestController
public class CommonController {

    @Autowired
    private SmsTemplate smsTemplate;

    @Autowired
    private RedisTemplate redisTemplate;

    //发送短信
    @GetMapping("/common/smsMsg")
    public ResponseResult sendMsg(String phone) {
      // 1.生成六位随机数
        //String code = RandomUtil.randomNumbers(6);
        String code = "123";
      // 2.调用阿里云sms发短信
        smsTemplate.sendSms(phone,code);
      // 3.将验证码存入redis （5分钟）
        redisTemplate.opsForValue().set("code:"+phone, code, Duration.ofSeconds(300));
      //返回
      return ResponseResult.okResult();
}
}
